package com.example.springbeanlife;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName SimulateAopConfi
 * @Description
 * @Author Yin
 * @Date 2021/4/8 15:01
 * @Version 1.0
 */
public class SimulateAopConfig implements InstantiationAwareBeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(beanName.equals("userService")){
            return Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("代理逻辑");
                    method.invoke(bean,args);
                    System.out.println("代理逻辑");
                    return null;
                }
            });
        }
        return null;
    }
}
